package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.view.LayoutInflater;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencySaveAgencyListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.SaveAgency1Bean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencySaveAgency1Activity extends CommonListActivity implements CallBack<DateBean> {
    private MySpinner spYear;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencySaveAgencyListAdapter(this);
    }

    @Override
    public void initView() {
        super.initView();

        View itemView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_save_agency_top, null);
        spYear = itemView.findViewById(R.id.spYear);
        flContent.addView(itemView);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_SAVE_AGENCY_1;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(EmergencySaveAgency2Activity.class);
            }
        });
        requestData(TimeUtils.dateStringY(System.currentTimeMillis()));
        spYear.setmDatas(TimeUtils.getYearList(), new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {
                requestData(TimeUtils.getYearList().get(position));
            }
        });
    }

    @Override
    public void onBack(DateBean dateBean) {
        spYear.getmText().setText(dateBean.getmYear()+"");
        requestData(dateBean.getmYear() + "");
    }

    private void requestData(String year) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_SAVE_AGENCY_1);
        HashMap<String, String> map = new HashMap<>();
        map.put("year", year);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<SaveAgency1Bean> saveAgency1BeanList = GsonUtil.jsonToList(listArray.toString(), SaveAgency1Bean.class);
                mAdapter.updateItems(saveAgency1BeanList);
            }
        });
    }

}
